package com.stillalive.springioc;

import com.stillalive.springioc.component.StudentComponent;
import com.stillalive.springioc.component.UserComponent;
import com.stillalive.springioc.config.UserConfig;
import com.stillalive.springioc.controller.HelloController;
import com.stillalive.springioc.controller.YmlController;
import com.stillalive.springioc.model.Student;
import com.stillalive.springioc.rep.UserRepository;
import com.stillalive.springioc.service.UserService;
import lombok.val;
import org.apache.naming.factory.BeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringiocApplication {

    public static void main(String[] args) {
        // 启动Spring项目，并扫描这个项目，将带有@Component注解的类加载为Bean并返回给context
        ApplicationContext context = SpringApplication.run(SpringiocApplication.class, args);
//        // 第一种方法，通过类名或的对象
//        HelloController bean = context.getBean(HelloController.class);
//        bean.sayHello();
//        // 第二种方法，通过对象名
//        HelloController bean2 = (HelloController) context.getBean("helloController");
//        bean2.sayHello();
//        // 第三种方法，对象名和类名
//        HelloController bean3 = context.getBean("hello", HelloController.class);
//        bean3.sayHello();
//        UserService bean = context.getBean(UserService.class);
//        bean.doService();
//        UserComponent bean1 = context.getBean(UserComponent.class);
//        bean1.doComponent();
//        UserConfig bean2 = context.getBean(UserConfig.class);
//        bean2.doConfig();
//        UserRepository bean3 = context.getBean(UserRepository.class);
//        bean3.doRepository();
//        // 也可以通过注解指定对象名
//        HelloController bean4 = (HelloController) context.getBean("hello");
//        bean4.sayHello();
//        // 通过@Bean注解可以获得类的不同对象
//        Student student = (Student) context.getBean("s3");
//        System.out.println(student);
//        student = (Student) context.getBean("s2");
//        System.out.println(student);
    }
}
